package cn.iocoder.yudao.module.system.dal.mysql.bookcollection;

import java.util.*;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.dal.dataobject.bookcollection.BookcollectionBookDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * 绘本集-绘本关联 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface BookcollectionBookMapper extends BaseMapperX<BookcollectionBookDO> {

    /**
     * 根据绘本集ID查询关联的绘本ID列表
     *
     * @param collectionId 绘本集ID
     * @return 绘本ID列表
     */
    default List<Long> selectBookIdsByCollectionId(Long collectionId) {
        return selectList(BookcollectionBookDO::getCollectionId, collectionId)
                .stream()
                .map(BookcollectionBookDO::getBooksId)
                .collect(java.util.stream.Collectors.toList());
    }

    /**
     * 根据绘本ID查询关联的绘本集ID列表
     *
     * @param bookId 绘本ID
     * @return 绘本集ID列表
     */
    default List<Long> selectCollectionIdsByBookId(Long bookId) {
        return selectList(BookcollectionBookDO::getBooksId, bookId)
                .stream()
                .map(BookcollectionBookDO::getCollectionId)
                .collect(java.util.stream.Collectors.toList());
    }

    /**
     * 批量插入关联记录
     *
     * @param collectionId 绘本集ID
     * @param bookIds 绘本ID列表
     * @return 插入数量
     */
    default int insertBatch(Long collectionId, List<Long> bookIds) {
        if (bookIds == null || bookIds.isEmpty()) {
            return 0;
        }
        
        List<BookcollectionBookDO> bookcollectionBooks = new ArrayList<>();
        for (Long bookId : bookIds) {
            BookcollectionBookDO bookcollectionBook = new BookcollectionBookDO();
            bookcollectionBook.setCollectionId(collectionId);
            bookcollectionBook.setBooksId(bookId);
            bookcollectionBooks.add(bookcollectionBook);
        }
        
        for (BookcollectionBookDO bookcollectionBook : bookcollectionBooks) {
            insert(bookcollectionBook);
        }
        return bookcollectionBooks.size();
    }

    /**
     * 根据绘本集ID删除关联记录
     *
     * @param collectionId 绘本集ID
     * @return 删除数量
     */
    default int deleteByCollectionId(Long collectionId) {
        return delete(BookcollectionBookDO::getCollectionId, collectionId);
    }

    /**
     * 根据绘本ID删除关联记录
     *
     * @param bookId 绘本ID
     * @return 删除数量
     */
    default int deleteByBookId(Long bookId) {
        return delete(BookcollectionBookDO::getBooksId, bookId);
    }

    /**
     * 检查绘本是否已关联到指定绘本集
     *
     * @param collectionId 绘本集ID
     * @param bookId 绘本ID
     * @return 是否存在关联
     */
    default boolean existsByCollectionIdAndBookId(Long collectionId, Long bookId) {
        return selectCount(new LambdaQueryWrapperX<BookcollectionBookDO>()
                .eq(BookcollectionBookDO::getCollectionId, collectionId)
                .eq(BookcollectionBookDO::getBooksId, bookId)) > 0;
    }

} 